package com.leyou.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.PageDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     * 根据品牌id集合查询品牌集合
     *
     * @param ids
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(ids)));
    }


    /**
     * 根据id查询品牌
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    private ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }


    /**
     * 根据分类id查询品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id") Long id) {

        return ResponseEntity.ok(this.brandService.queryBrandByCategory(id));
    }

    /**
     * 分页查询品牌
     * @param page  页码
     * @param rows  每页显示记录数
     * @param key  搜索条件
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "4") Integer rows,
            @RequestParam(value = "key", required = false) String key
            ) {
        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows, key));
    }


    /**
     * 新增品牌(在新增Brand表的同时也要在中间表中添加,在实现类理念实现)
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 更新品牌
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id){
        //根据id删除品牌
        brandService.removeById(id);
        //删除Brand表数据的同时,也要删除中间表
        categoryBrandService.remove(
                new QueryWrapper<CategoryBrand>().eq("brand_id", id)
        );
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }
}
